package kr.or.eclipse.democamp;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import kr.or.eclipse.democamp.actions.Action;
import kr.or.eclipse.democamp.actions.Activate;
import kr.or.eclipse.democamp.actions.ConfirmMail;

@SuppressWarnings("serial")
public class DemoCampServlet extends HttpServlet {
	private Map<String, Action> actionMap = new HashMap<String, Action>();

	public DemoCampServlet() {
		addAction(new ConfirmMail());
		addAction(new Activate());
	}

	private void addAction(Action action) {
		actionMap.put(action.getActionName(), action);
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		try {
			doPost(req, resp);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String actionName = req.getParameter("action");
		Action action = actionMap.get(actionName);
		resp.setCharacterEncoding("UTF-8");
		if (action != null) {
			action.doAction(req, resp);
		}

	}

}
